<template>
  <Dialog
    :title="title"
    :model-value="visible"
    width="800px"
    max-height="600px"
    :before-close="closeForm"
  >
    <Form
      :schema="schema"
      :model="inter"
      @register="formRegister"
      :isCol="false"
      label-width="120px"
    />
  </Dialog>
</template>

<script>
import { EnumDic } from '@/flow/config/flowConfig'
import * as interApi from '@/flow/api/flowInter'
import * as serverApi from '@/flow/api/server'
import { Dialog } from '@/components/Dialog'
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
const { formRegister, formMethods } = useForm()
export default {
  components: {
    Dialog,
    Form
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      EnumDic,
      loading: false,
      title: '查看流程事件',
      schema: [],
      groups: [],
      servers: [],
      inter: {}
    }
  },
  computed: {},
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  methods: {
    formRegister,
    initSceme() {
      const list = [
        {
          field: 'InterName',
          label: '事件名',
          component: 'Input',
          componentProps: {
            readonly: true,
            placeholder: '事件名'
          }
        },
        {
          field: 'UseRange',
          label: '事件类型',
          component: 'EnumItem',
          componentProps: {
            readonly: true,
            dicKey: EnumDic.FlowUseRange,
            placeholder: '事件类型',
            sysHead: 'flow'
          }
        },
        {
          field: 'InterType',
          label: '通信类型',
          component: 'EnumItem',
          componentProps: {
            readonly: true,
            dicKey: EnumDic.FlowInterType,
            placeholder: '通信类型',
            sysHead: 'flow'
          }
        }
      ]
      if (this.inter.InterType == 1) {
        list.push({
          field: 'http',
          label: 'HTTP接口配置',
          component: 'Divider'
        })
        list.push({
          field: 'Uri',
          label: '请求完整URI',
          component: 'Input',
          componentProps: {
            readonly: true,
            placeholder: '请求完整URI'
          }
        })
        if (this.inter.Uri && this.inter.Uri.startsWith('https:')) {
          list.push({
            field: 'SslProtocols',
            label: 'SSL协议类型',
            component: 'Select',
            componentProps: {
              disabled: true,
              options: [
                {
                  label: 'Ssl2',
                  value: 12
                },
                {
                  label: 'Ssl3',
                  value: 48
                },
                {
                  label: 'Tls',
                  value: 192
                },
                {
                  label: 'Default',
                  value: 240
                },
                {
                  label: 'Tls11',
                  value: 768
                },
                {
                  label: 'Tls12',
                  value: 3072
                },
                {
                  label: 'Tls13',
                  value: 12288
                }
              ],
              placeholder: 'SSL协议类型'
            }
          })
        }
        list.push({
          field: 'AutomaticDecompression',
          label: '解压方式',
          component: 'Select',
          componentProps: {
            disabled: true,
            options: [
              {
                label: 'All',
                value: -1
              },
              {
                label: 'GZip',
                value: 1
              },
              {
                label: 'Deflate',
                value: 2
              },
              {
                label: 'Brotli',
                value: 4
              }
            ],
            placeholder: '解压方式'
          }
        })
        list.push({
          field: 'Uri',
          label: '请求完整URI',
          component: 'Input',
          componentProps: {
            readonly: true,
            placeholder: '请求完整URI'
          }
        })
        list.push({
          field: 'AllowAutoRedirect',
          label: '允许自动重定向',
          component: 'Switch',
          componentProps: {
            disabled: true
          }
        })
        if (this.inter.AllowAutoRedirect) {
          list.push({
            field: 'MaxAutomaticRedirections',
            label: '重定向最大数目',
            component: 'InputNumber',
            componentProps: {
              placeholder: '重定向最大数目',
              disabled: true
            }
          })
        }
      } else {
        list.push({
          field: 'RpcConfig',
          label: this.inter.InterType === 2 ? 'RPC多播配置' : 'RPC接口配置',
          component: 'Divider'
        })
        list.push({
          field: 'SysDictate',
          label: '指令',
          component: 'Input',
          componentProps: {
            readonly: true,
            placeholder: '指令'
          }
        })
        list.push({
          field: 'ToType',
          label: '目的地',
          component: 'Select',
          componentProps: {
            disabled: true,
            options: [
              {
                label: '服务节点类型',
                value: 0
              },
              {
                label: '固定服务节点',
                value: 1
              }
            ],
            placeholder: '目的地'
          }
        })
        if (this.inter.ToType === 0) {
          list.push({
            field: 'SystemType',
            label: '服务节点类型',
            component: 'Select',
            componentProps: {
              disabled: true,
              options: this.groups,
              placeholder: '服务节点类型',
              multiple: this.inter.InterType === 2,
              clearable: true
            }
          })
        } else {
          list.push({
            field: 'ServerId',
            label: '固定服务节点',
            component: 'Select',
            componentProps: {
              disabled: true,
              options: this.servers,
              valueKey: 'ServerId',
              props: {
                value: 'ServerId',
                label: 'ServerName'
              },
              placeholder: '固定服务节点',
              clearable: true
            }
          })
        }
        list.push({
          field: 'IsEnableLock',
          label: '是否启用锁',
          component: 'Switch',
          componentProps: {
            disabled: true
          }
        })
        if (this.inter.InterType === 0) {
          list.push({
            field: 'Transmit',
            label: '负载均衡方案名',
            component: 'Input',
            componentProps: {
              readonly: true,
              placeholder: '负载均衡方案名'
            }
          })
        }
      }
      this.schema = list
    },
    async loadServer() {
      if (this.servers.length === 0) {
        const list = await serverApi.GetServers()
        this.servers = list
      }
    },
    async loadGroup() {
      if (this.groups.length === 0) {
        const list = await serverApi.Gets()
        this.groups = list.map((c) => {
          return {
            value: c.Id,
            label: c.GroupName,
            options: c.ServerType.map((c) => {
              return {
                value: c.TypeVal,
                label: c.SystemName
              }
            })
          }
        })
      }
    },
    closeForm() {
      this.$emit('update:visible', false)
    },
    async reset() {
      await this.loadGroup()
      await this.loadServer()
      const res = await interApi.Get(this.id)
      this.title = '查看流程事件:' + res.InterName
      const data = {
        InterName: res.InterName,
        InterType: res.InterType,
        UseRange: res.UseRange
      }
      if (res.InterType === 0 && res.InterConfig && res.InterConfig.RpcConfig) {
        this.inter = Object.assign(data, res.InterConfig.RpcConfig)
        this.inter.ToType = this.inter.ServerId != null ? 1 : 0
      } else if (res.InterType === 1 && res.InterConfig && res.InterConfig.HttpConfig) {
        this.inter = Object.assign(data, res.InterConfig.HttpConfig)
      } else if (res.InterType === 2 && res.InterConfig && res.InterConfig.BroadcastConfig) {
        this.inter = Object.assign(data, res.InterConfig.BroadcastConfig)
        this.inter.ToType = this.inter.ServerId != null ? 1 : 0
      } else {
        this.inter = data
      }
      formMethods.setValues(this.inter)
      this.initSceme()
    }
  }
}
</script>
